package com.chilli.cosima.action.logmanager;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;

import com.chilli.cosima.action.BaseSupport;
import com.chilli.cosima.common.ErrorBean;

public class RunLogEditAction extends BaseSupport
{

	/**
	 * 注释内容
	 */
	private static final long serialVersionUID = 6614149229541469784L;

	private Log log = LogFactory.getLog(RunLogEditAction.class);

	private String content;

	public String getContent()
	{
		return content;
	}

	public void setContent(String content)
	{
		this.content = content;
	}

	public String jumpPage()
	{
		String filePath = Thread.currentThread().getContextClassLoader()
				.getResource("/log4j.properties").getPath();
		File file = new File(filePath);
		if (!file.exists() || !file.isFile())
		{
			log.info("The log4j.properties file (" + filePath
					+ ") is not exist.");
			ErrorBean bean = new ErrorBean("log4j日志配置文件不存在，请检查log4j配置文件。",
					"runlog/runLogQueryAction.action");
			request.setAttribute("errorBean", bean);
			return ERROR;
		}

		BufferedReader reader = null;
		try
		{
			reader = new BufferedReader(new FileReader(file));
			StringBuilder sb = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null)
			{
				sb.append(line).append("\r\n");
			}

			setContent(sb.toString());

			return JUMP_PAGE;
		} catch (Exception e)
		{
			log.error(e.getMessage(), e);
		} finally
		{
			if (reader != null)
			{
				try
				{
					reader.close();
				} catch (IOException e)
				{
					log.error(e.getMessage(), e);
				}
			}
		}

		ErrorBean bean = new ErrorBean("log4j日志配置文件修改失败。",
				"runlog/runLogQueryAction.action");
		request.setAttribute("errorBean", bean);
		return ERROR;

	}

	public String execute()
	{
		String filePath = Thread.currentThread().getContextClassLoader()
				.getResource("/log4j.properties").getPath();
		File file = new File(filePath);
		if (!file.exists() || !file.isFile())
		{
			log.info("The log4j.properties file (" + filePath
					+ ") is not exist.");
			ErrorBean bean = new ErrorBean("log4j日志配置文件不存在，请检查log4j配置文件。",
					"runlog/runLogQueryAction.action");
			request.setAttribute("errorBean", bean);
			return ERROR;
		}

		BufferedWriter writer = null;
		try
		{
			writer = new BufferedWriter(new FileWriter(file));
			writer.write(getContent());
			writer.flush();

			PropertyConfigurator.configure(filePath);
			log.info("modify log4j.properties successfully.");
			return SUCCESS;
		} catch (IOException e)
		{
			log.error(e.getMessage(), e);
		} finally
		{
			if (writer != null)
			{
				try
				{
					writer.close();
				} catch (IOException e)
				{
					log.error(e.getMessage(), e);
				}
			}
		}

		ErrorBean bean = new ErrorBean("log4j日志配置文件修改失败。",
				"runlog/runLogQueryAction.action");
		request.setAttribute("errorBean", bean);
		return ERROR;

	}

}
